﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win.CSkin.Test
{
    public partial class frmWaitingBox : Form
    {
        /// <summary>
        /// 是否等待
        /// </summary>
        public bool waiting = true;
        /// <summary>
        /// 文字描述
        /// </summary>
        public string Desc = string.Empty;

        public frmWaitingBox(string message)
        {
            InitializeComponent();

            if (!string.IsNullOrWhiteSpace(message))
            {
                loadingCtrl.Text = message;
                //Desc = message;
            }

            loadingCtrl.Show();

            Task.Factory.StartNew(() =>
            {
                Waiting();
            });

        }

        /// <summary>
        /// 显示等待控件，检测状态关闭页面
        /// </summary>
        private void Waiting()
        {
            while (true)
            {
                Thread.Sleep(100);
                if (!this.IsHandleCreated)
                {
                    continue;
                }
                this.BeginInvoke((Action)(() =>
                {
                    if (!waiting)
                    {
                        //this.Dispose();
                        this.Close();
                    }
                    if (!string.IsNullOrWhiteSpace(Desc))
                    {
                        if (Desc != loadingCtrl.Text)
                            loadingCtrl.Text = Desc;
                    }
                }));
            }
        }

    }
}
